home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_018 / amigadisplay / dpy.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  269 lines

  1. /****************************************\
  2. * This file controls the display (dpy.c) *
  3. \****************************************/
  4.  
  5. #include <exec/types.h>
  6. #include <graphics/gfxbase.h>
  7. #include <graphics/gfx.h>
  8. #include <graphics/text.h>
  9. #include <stdio.h>
  10. #include <intuition/intuitionbase.h>
  11. #include <intuition/intuition.h>
  12.  
  13. extern struct IntuitionBase *IntuitionBase;
  14. extern struct GfxBase       *GfxBase;
  15.  
  16. extern Beep();
  17.  
  18. static short x, y, mode, parse, saved_arg, dpytype, autocr;
  19.  
  20. static struct RastPort *rp;
  21. static struct Window *w;
  22.  
  23. #define ctrl  0x1F  /* turns letter into ctrl char */
  24. #define INVIS 0x80 /* returned by Emit for non-printed chars */
  25.  
  26. /* Screen dimensions */
  27.  
  28. #define LEFT    0
  29. #define RIGHT   LEFT+8*80-1
  30. #define TOP     4 /* overlapped by menu stripe, but so what */
  31. #define BOTTOM  TOP+8*24-1
  32.  
  33. /* Mode bits */
  34.  
  35. #define EMPH    (1<<0)  /* emphasis (implemented as alternate background) */
  36. #define BLINK   (1<<1)  /* blinking (implemented as colored text) */
  37. #define INSDEL  (1<<2)  /* insert/delete mode */
  38. #define ROLL    (1<<3)  /* what to do when cursor moves down from last line */
  39.  
  40. /* Parse-state bits */
  41.  
  42. #define NORMAL   0
  43. #define QUOTING  1  /* next char is displayed even if special */
  44. #define CURSOR   2  /* next 2 chars specify new cursor loc */
  45. #define CURSOR2  3  /* first cursor-loc char is in saved_arg */
  46.  
  47. /* Display types */
  48.  
  49. #define GLASS  0  /* vanilla, handles BS, FF, and BEL specially */
  50. #define SAIL   1  /* DM with blink/bold swapped & 128 possible chars */
  51. #define MAX_TYPE  1
  52.  
  53. /*
  54.  * Initialisation
  55.  */
  56.  
  57. SetUpDisplay(window, type) struct Window *window; int type; {
  58.    if (type >= 0 && type <= MAX_TYPE) dpytype = type;
  59.    if (window) {
  60.       Move(rp=((w=window)->RPort), x=LEFT, (y=TOP)+6);
  61.       ClearScreen(rp);
  62.       Cursor();
  63.       }
  64.    parse = NORMAL;
  65.    mode = ROLL;
  66.    autocr = FALSE;
  67.    }
  68.  
  69. /*
  70.  * Querying re special SAIL chars
  71.  *
  72.  * Certain control chars have special effects when sent to the Stanford host.
  73.  * Since these special effects are being provided by certain function keys,
  74.  * the control chars themselves can be interpreted as ordinary type-in.  To
  75.  * do this, you have to ship Stanford a special prefix char.  This function
  76.  * lets the main loop test whether such a prefix is needed.
  77.  */
  78.  
  79. NeedQuote(c) char c; {
  80.    if (dpytype == SAIL) switch (c) {
  81.       case ctrl&'C':
  82.       case ctrl&'^':
  83.       case ctrl&'_':
  84.          return(TRUE);
  85.       }
  86.    return(FALSE);
  87.    }
  88.  
  89. /*
  90.  * The Guts
  91.  *
  92.  * Process one output char; returns char to go into log file (INVIS if none)
  93.  */
  94.  
  95. static char RealEmit(c) char c; {
  96.    if (autocr) switch (c) { /* suppress explicit CR/LF after wrapping around */
  97.       case ctrl&'M': return(INVIS);
  98.       case ctrl&'J': {autocr = FALSE; return(c);};
  99.       default: autocr = FALSE;
  100.       }
  101.    if (parse == CURSOR || parse == CURSOR2) {
  102.       switch (c) {
  103.          case ctrl&'B':
  104.          case ctrl&'L':
  105.          case ctrl&'Q':
  106.          case ctrl&'R':
  107.          case ctrl&'X':
  108.          case ctrl&'^':
  109.          case ctrl&'_':
  110.             parse = NORMAL; /* new control char overrides cursor command */
  111.             break;
  112.          default:
  113.             if (parse == CURSOR) {
  114.                saved_arg = c^0x60;
  115.                parse = CURSOR2;
  116.                return(INVIS);
  117.                }
  118.             x = min(79, saved_arg&0xFF) * 8 + LEFT;
  119.             y = min(23, (c^0x60)&0xFF) * 8 + TOP;
  120.             parse = NORMAL;
  121.             return(INVIS);
  122.          }
  123.       }
  124.    if ((c >= ' ' && c <= '~') || parse == QUOTING) { /* display this char */
  125.       if (mode & INSDEL && x < RIGHT-8)
  126.          ScrollRaster(rp, -8, 0, x, y, RIGHT, y+7);
  127.       Show(c);
  128.       parse = NORMAL;
  129.       return(c);
  130.       }
  131.    switch (c) {
  132.       case ctrl&'B': /* home cursor */
  133.          if (dpytype == SAIL) {x = LEFT; y = TOP;}
  134.          break;
  135.       case ctrl&'G': /* beep */
  136.          if (Beep()) DisplayBeep(w->WScreen);
  137.          break;
  138.       case ctrl&'H': /* backspace */
  139.          if (! (mode & INSDEL)) x = max(x-8, LEFT);
  140.          else if (x < RIGHT-8) ScrollRaster(rp, 8, 0, x, y, RIGHT, y+7);
  141.          else {Move(rp, x, y+6); ClearEOL(rp);}
  142.          break;
  143.       case ctrl&'I': /* tab */
  144.          x = ((x-LEFT) / 64 + 1) * 64 + LEFT;
  145.          if (x <= RIGHT) return(c);
  146.          x = LEFT;
  147.       case ctrl&'J': /* line feed (or tab overflow) */
  148.          if (! (mode & INSDEL)) {DownLine(); return(c);}
  149.          if (y < BOTTOM-8) ScrollRaster(rp, 0, -8, LEFT, y, RIGHT, BOTTOM);
  150.          else {Move(rp, LEFT, y+6); ClearEOL(rp);}
  151.          break;
  152.       case ctrl&'L': /* clear screen or set cursor */
  153.          if (dpytype == SAIL) parse = CURSOR;
  154.          else {
  155.             Move(rp, x=LEFT, (y=TOP)+6);
  156.             ClearScreen(rp);
  157.             }
  158.          break;
  159.       case ctrl&'M': /* carriage return (or excess tab) */
  160.          x = LEFT;
  161.          break;
  162.       case ctrl&'N': /* turn on bold */
  163.          if (dpytype == SAIL) mode |= EMPH;
  164.          break;
  165.       case ctrl&'O': /* turn on blinking */
  166.          if (dpytype == SAIL) mode |= BLINK;
  167.          break;
  168.       case ctrl&'P': /* turn on insert/delete mode */
  169.          if (dpytype == SAIL) mode |= INSDEL;
  170.          break;
  171.       case ctrl&'W': /* erase to EOL */
  172.          if (dpytype == SAIL) {
  173.             Move(rp, x, y+6);
  174.             ClearEOL(rp);
  175.             }
  176.          break;
  177.       case ctrl&'X': /* cancel modes */
  178.          if (dpytype == SAIL) mode = 0;
  179.          break;
  180.       case ctrl&'Z': /* cursor up */
  181.          if (dpytype != SAIL) break;
  182.          if (! (mode & INSDEL)) y = max(TOP, y-8);
  183.          else if (y < BOTTOM-8) ScrollRaster(rp, 0, 8, LEFT, y, RIGHT, BOTTOM);
  184.          else {Move(rp, LEFT, y+6); ClearEOL(rp);}
  185.          break;
  186.       case ctrl&'[': /* quote next character */
  187.          if (dpytype == SAIL) parse = QUOTING;
  188.          break;
  189.       case ctrl&'\\': /* cursor right */
  190.          if (dpytype != SAIL) break;
  191.          if (mode & INSDEL) {
  192.             if (x < RIGHT-8) ScrollRaster(rp, -8, 0, x, y, RIGHT, y+7);
  193.             else {Move(rp, x, y+6); ClearEOL(rp);}
  194.             }
  195.          else if ((x += 8) > RIGHT) {
  196.             x = LEFT;
  197.             if ((y += 8) > BOTTOM) y = TOP;
  198.             }
  199.          break;
  200.       case ctrl&']': /* turn on roll mode */
  201.          if (dpytype == SAIL) mode |= ROLL;
  202.          break;
  203.       case ctrl&'^': /* master clear */
  204.       case ctrl&'_': /* clear unprotected text */
  205.          if (dpytype != SAIL) break;
  206.          mode &= ~(BLINK | EMPH | INSDEL);
  207.          Move(rp, x=LEFT, (y=TOP)+6);
  208.          ClearScreen(rp);
  209.          break;
  210.       } /* end of switch (c) for control chars */
  211.    return(INVIS);
  212.    } /* end of RealEmit */
  213.  
  214. /*
  215.  * Various screen-action functions
  216.  */
  217.  
  218. static Show(c) char c; {
  219.    Move(rp, x, y+6);
  220.    if (c < ' ') c += 0x80; /* chars 00-1F are hidden in 80-9F in the font */
  221.    if (mode & EMPH) SetBPen(rp, 2); /* emphasis by changing background */
  222.    if (mode & BLINK) SetAPen(rp, 3); /* "blink" by changing foreground */
  223.    Text(rp, &c, 1);
  224.    if (mode & EMPH) SetBPen(rp, 0);
  225.    if (mode & BLINK) SetAPen(rp, 1);
  226.    if ((x += 8) > RIGHT) {
  227.       x = LEFT;
  228.       DownLine();
  229.       autocr = TRUE;
  230.       }
  231.    }
  232.  
  233. static DownLine() {
  234.    if ((y += 8) > BOTTOM) {
  235.       y -= 8;
  236.       if (mode & ROLL) ScrollRaster(rp, 0, 8, LEFT, TOP, RIGHT, BOTTOM);
  237.       else y = TOP;
  238.       }
  239.    }
  240.  
  241. static Cursor() {
  242.    SetAPen(rp, 3);
  243.    SetDrMd(rp, COMPLEMENT);
  244.    RectFill(rp, x, y, x+7, y+7);
  245.    SetDrMd(rp, JAM2);
  246.    SetAPen(rp, 1);
  247.    }
  248.  
  249. /*
  250.  * Public function to process one char
  251.  */
  252.  
  253. char Emit(c) char c; {
  254.    Cursor();
  255.    c = RealEmit(c & 0x7F);
  256.    Cursor();
  257.    return(c);
  258.    }
  259.  
  260. /*
  261.  * Public function to process a string of chars (not currently used)
  262.  */
  263.  
  264. Emits(string) char *string; {
  265.    Cursor();
  266.    while (*string) RealEmit(*string++ & 0x7F);
  267.    Cursor();
  268.    }
  269.